package com.fc.test.service;

import java.util.List;
import java.util.Arrays;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fc.test.mapper.auto.EntInfoMapper;
import com.fc.test.mapper.auto.EntJobMapper;
import com.fc.test.mapper.auto.StuInfoMapper;
import com.fc.test.model.auto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.ConvertUtil;
import com.fc.test.mapper.auto.InterviewMapper;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import com.fc.test.util.StringUtils;

/**
 * 企业面试流程 InterviewService
 * @Title: InterviewService.java 
 * @Package com.fc.test.service 
 * @author DrRic_自动生成
 * @email drhyz@qq.com
 * @date 2020-07-05 17:21:25  
 **/
@Service
public class InterviewService implements BaseService<Interview, InterviewExample>{
	@Autowired
	private InterviewMapper interviewMapper;
	@Autowired
	private StuInfoMapper stuInfoMapper;
	@Autowired
	private EntInfoMapper entInfoMapper;
	@Autowired
	private EntJobMapper entJobMapper;
      	   	      	      	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<Object> list(Tablepar tablepar,Interview record){
	        InterviewExample testExample=new InterviewExample();
	        if(record.getEid()!=null){
				testExample.createCriteria().andEidEqualTo(record.getEid());
				testExample.setOrderByClause("id ASC");
				if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
					testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
				}
				PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
				List<Interview> list= interviewMapper.selectByExample(testExample);
				JSONArray result = new JSONArray();
				for(Interview interview : list){
					JSONObject rec = new JSONObject();
					rec.put("id", interview.getId());
					rec.put("sid", interview.getSid());
					rec.put("eid", interview.getEid());
					rec.put("jid", interview.getJid());
					rec.put("status", interview.getStatus());
					rec.put("evaluate", interview.getEvaluate());
					rec.put("way", interview.getWay());
					rec.put("istatus", interview.getIstatus());

					StuInfo stuInfo = stuInfoMapper.selectByPrimaryKey(interview.getSid());
					EntInfo entInfo = entInfoMapper.selectByPrimaryKey(interview.getEid());
					EntJob entJob = entJobMapper.selectByPrimaryKey(interview.getJid());

					rec.put("stuname", stuInfo!=null?stuInfo.getName():"");
					rec.put("sex", stuInfo!=null?stuInfo.getSex():-1);
					rec.put("school", stuInfo!=null?stuInfo.getSchool():"");
					rec.put("education", stuInfo!=null?stuInfo.getEducation():"");
					rec.put("college", stuInfo!=null?stuInfo.getCollege():"");
					rec.put("major", stuInfo!=null?stuInfo.getMajor():"");

					rec.put("jobname", entJob!=null?entJob.getName():"");

					rec.put("entname", entInfo!=null?entJob.getName():"");

					result.add(rec);
				}

				return new PageInfo<Object>(result);
			}
	        else if(record.getSid()!=null){
				testExample.createCriteria().andSidEqualTo(record.getSid());
				testExample.setOrderByClause("id ASC");
				if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
					testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
				}
				PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
				List<Interview> list= interviewMapper.selectByExample(testExample);
				JSONArray result = new JSONArray();
				for(Interview interview : list){
					JSONObject rec = new JSONObject();
					rec.put("id", interview.getId());
					rec.put("sid", interview.getSid());
					rec.put("eid", interview.getEid());
					rec.put("jid", interview.getJid());
					rec.put("status", interview.getStatus());
					rec.put("evaluate", interview.getEvaluate());
					rec.put("way", interview.getWay());
					rec.put("istatus", interview.getIstatus());

					StuInfo stuInfo = stuInfoMapper.selectByPrimaryKey(interview.getSid());
					EntInfo entInfo = entInfoMapper.selectByPrimaryKey(interview.getEid());
					EntJob entJob = entJobMapper.selectByPrimaryKey(interview.getJid());

					rec.put("stuname", stuInfo!=null?stuInfo.getName():"");
					rec.put("school", stuInfo!=null?stuInfo.getSchool():"");
					rec.put("education", stuInfo!=null?stuInfo.getEducation():"");
					rec.put("college", stuInfo!=null?stuInfo.getCollege():"");
					rec.put("major", stuInfo!=null?stuInfo.getMajor():"");

					rec.put("jobname", entJob!=null?entJob.getName():"");

					rec.put("entname", entInfo!=null?entJob.getName():"");

					result.add(rec);
				}

				return new PageInfo<Object>(result);
			}
	        else {
	        	return new PageInfo<>();
			}

	 }

	public PageInfo<Interview> listForSys(Tablepar tablepar, Interview record){
		InterviewExample testExample=new InterviewExample();
		testExample.createCriteria().andLikeQuery(record);
		testExample.setOrderByClause("id ASC");
		if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
			testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
		}
		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		List<Interview> list= interviewMapper.selectByExample(testExample);
		PageInfo<Interview> pageInfo = new PageInfo<Interview>(list);
		return  pageInfo;
	}


	@Override
	public int deleteByPrimaryKey(String ids) {
					
			Integer[] integers = ConvertUtil.toIntArray(",", ids);
			List<Integer> stringB = Arrays.asList(integers);
			InterviewExample example=new InterviewExample();
			example.createCriteria().andIdIn(stringB);
			return interviewMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public Interview selectByPrimaryKey(String id) {
				
			Integer id1 = Integer.valueOf(id);
			return interviewMapper.selectByPrimaryKey(id1);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(Interview record) {
		return interviewMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(Interview record) {
				
		record.setId(null);
		
				
		return interviewMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(Interview record, InterviewExample example) {
		
		return interviewMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(Interview record, InterviewExample example) {
		
		return interviewMapper.updateByExample(record, example);
	}

	@Override
	public List<Interview> selectByExample(InterviewExample example) {
		
		return interviewMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(InterviewExample example) {
		
		return interviewMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(InterviewExample example) {
		
		return interviewMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param interview
	 * @return
	 */
	public int checkNameUnique(Interview interview){
		InterviewExample example=new InterviewExample();
		example.createCriteria().andStatusEqualTo(interview.getStatus());
		List<Interview> list=interviewMapper.selectByExample(example);
		return list.size();
	}


}
